<?php

declare(strict_types=1);

namespace App\Controller\Common;

use App\Entity\ApiDoc;
use App\Utils\Annotation\ApiName;
use App\Utils\Traits\ControllerTrait;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
 * Class DocumentController
 * @package App\Controller\Common
 * @Route("/common/document", name="common_document_")
 * @ApiName(name="Api文档")
 */
class DocumentController extends AbstractController {
    use ControllerTrait;

    /**
     * @ApiName(name="接口文档", description="获取API接口文档")
     * @Route("/api", methods={"GET"}, name="api")
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function api (Request $request) {
        if (is_prod_env()) {
            access_denied_exception();
        }
        $api_doc = $this->getDoctrine()->getRepository(ApiDoc::class)->findOneBy([], ['id' => 'DESC']);
        if (!$api_doc) {
            exception('接口文档未生成');
        }
        return $this->render('Document/api.html.twig', [
            'website_name' => $this->getParameter('website.name'),
            'website_logo' => $this->getParameter('website.logo'),
            'version' => $api_doc->getId(),
            'data'    => json_decode($api_doc->getContent(), true),
        ]);
    }
}